import apiClient from '../apiClient';
import {
  GetModelListReq,
  GetModelListRes,
  UpdateModelRes,
  GetProviderListRes,
  GetProviderListReq,
  GetCreatedListRes,
  GetCreatedListReq,
  CreateProviderReq,
  CreateProviderModelReq,
  DeleteProviderModelReq,
  TestModelReq,
  BatchTestRes,
  GetDefaultListRes,
  GetConnectionReq,
  UpdateActiveReq,
  CreateCustomReq,
  CustomTestReq,
} from '@/common/types';

export enum ModelApi {
  GetModelList = '/openapi/v2/models/list',
  UpdateModel = '/openapi/v2/models/update',
  GetFilterList = '/openapi/v2/models/filter/list',
  GetProviderList = '/openapi/v2/models/provider/list',
  GetCreatedList = '/openapi/v2/models/provider/created/list',
  CreateProvider = '/openapi/v2/models/provider/create',
  DeleteProvider = '/openapi/v2/models/provider/delete',
  UpdateProvider = '/openapi/v2/models/provider/update',
  CreateProviderModel = '/openapi/v2/models/provider/model/create',
  UpdateProviderModel = '/openapi/v2/models/provider/model/update',
  DeleteProviderModel = '/openapi/v2/models/provider/model/delete',
  GetDefaultList = '/openapi/v2/models/default/list',
  TestModel = '/openapi/v2/models/test',
  BatchTest = '/openapi/v2/models/batch/test',
  Connection = '/openapi/v2/models/connection',
  ChangeActive = '/openapi/v2/models/activate',
  CreateCustom = '/openapi/v2/models/provider/custom/create',
  CustomTest = '/openapi/v2/models/custom/test',
}

export const getModelList = (data: GetModelListReq) =>
  apiClient.post<GetModelListRes>({ url: ModelApi.GetModelList, data });

export const updateModel = (id: string, data: UpdateModelRes) =>
  apiClient.post({ url: `${ModelApi.UpdateModel}/${id}`, data });

export const getFilterList = () => apiClient.get({ url: ModelApi.GetFilterList });

export const getProviderList = (params: GetProviderListRes) =>
  apiClient.get<GetProviderListReq>({ url: ModelApi.GetProviderList, params });

export const getCreatedList = (params: GetCreatedListRes) =>
  apiClient.get<GetCreatedListReq>({ url: ModelApi.GetCreatedList, params });

export const createProvider = (data: CreateProviderReq) =>
  apiClient.post<string | null>({ url: ModelApi.CreateProvider, data });

export const createProviderModel = (data: CreateProviderModelReq) =>
  apiClient.post({ url: ModelApi.CreateProviderModel, data });

export const updateProviderModel = (data: CreateProviderModelReq) =>
  apiClient.post({ url: ModelApi.UpdateProviderModel, data });

export const deleteProviderModel = (data: DeleteProviderModelReq) =>
  apiClient.post({ url: ModelApi.DeleteProviderModel, data });

export const deleteProvider = (id: string) =>
  apiClient.post({ url: ModelApi.DeleteProvider + '/' + id });

export const updateProvider = (id: string, data: CreateProviderReq) =>
  apiClient.post({ url: ModelApi.UpdateProvider + `/${id}`, data });

export const getDefaultList = () =>
  apiClient.get<GetDefaultListRes>({ url: ModelApi.GetDefaultList });

export const testModel = (data: { name: string }) =>
  apiClient.post<TestModelReq>({ url: ModelApi.TestModel, data });

export const batchTestModel = (data: BatchTestRes) =>
  apiClient.post({ url: ModelApi.BatchTest, data });

export const getConnection = () => apiClient.post<GetConnectionReq>({ url: ModelApi.Connection });

export const updateActive = (id: string, data: UpdateActiveReq) =>
  apiClient.post({ url: ModelApi.ChangeActive + `/${id}`, data });

export const createCustom = (data: CreateCustomReq) =>
  apiClient.post({ url: ModelApi.CreateCustom, data });

export const customTest = (data: CustomTestReq) =>
  apiClient.post({ url: ModelApi.CustomTest, data });

export default {
  getModelList,
  updateModel,
  getFilterList,
  getProviderList,
  getCreatedList,
  createProvider,
  createProviderModel,
  updateProviderModel,
  deleteProviderModel,
  deleteProvider,
  updateProvider,
  getDefaultList,
  testModel,
  batchTestModel,
  getConnection,
  updateActive,
  createCustom,
  customTest,
};
